package com.googlecode.cswish.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TextUtil {

	private final static Log logger = LogFactory.getLog(TextUtil.class);
	
	public static String get(String content, String identity) {
		int index = content.indexOf(identity);
		if (index > 0) {
			int begin = content.lastIndexOf("<", index);		// TODO: USE "<@"?
			int tagNameEnd = content.indexOf(" ", begin);
			String tagName = content.substring(begin + 1, tagNameEnd);
			String closeTagName1 = "</" + tagName + ">";
			String closeTagName2 = "/>";
			
			// guess the end flag
			int end;
			int end1 = content.indexOf(closeTagName1, index + 1);
			int end2 = content.indexOf(closeTagName2, index + 1);
			if (end2 > end1) {
				// guess correctly
				end = end2 + closeTagName2.length() - 1;
			} else {
				// the tag is open tag, we need scan if there's child tag
				String openTagFlag = "<" + tagName + " ";
				
				// find the position which open tag count equals open tag count
				int openTagCount = 1;
				int closeTagCount = 0;
				int openIndex = begin;
				int closeIndex = begin;
				
				do {
					closeIndex = content.indexOf(closeTagName1, closeIndex + 1);
					if (closeIndex != -1) {
						closeTagCount++;
					} else {
						break;
					}
					openIndex = content.indexOf(openTagFlag, openIndex + 1);
					if (openIndex != -1) {
						openTagCount++;
					} else {
						break;
					}
				} while(!(openIndex > closeIndex || closeTagCount == openTagCount));
				
				end = closeIndex - 1;
				end = end + closeTagName1.length();
			}
			
			return content.substring(begin, end + 1);
		} else {
			return null;
		}
	}
	
	public static String replace(String content, String identity, String ctx, boolean onlyChild) {
		int index = content.indexOf(identity);
		if (index > 0) {
			int begin = content.lastIndexOf("<", index);		// TODO: USE "<@"?
			int tagNameEnd = content.indexOf(" ", begin);
			String tagName = content.substring(begin + 1, tagNameEnd);
			String closeTagName1 = "</" + tagName + ">";
			String closeTagName2 = "/>";
			
			// guess the end flag
			int end;
			int end1 = content.indexOf(closeTagName1, index + 1);
			int end2 = content.indexOf(closeTagName2, index + 1);
			if (end2 > end1) {
				// guess correctly
				end = end2 + closeTagName2.length();
			} else {
				// the tag is open tag, we need scan if there's child tag
				String openTagFlag = "<" + tagName + " ";
				
				// find the position which open tag count equals open tag count
				int openTagCount = 1;
				int closeTagCount = 0;
				int openIndex = begin;
				int closeIndex = begin;
				
				do {
					closeIndex = content.indexOf(closeTagName1, closeIndex + 1);
					if (closeIndex != -1) {
						closeTagCount++;
					} else {
						break;
					}
					openIndex = content.indexOf(openTagFlag, openIndex + 1);
					if (openIndex != -1) {
						openTagCount++;
					} else {
						break;
					}
				} while(!(openIndex > closeIndex || closeTagCount == openTagCount));
				
				end = closeIndex - 1;
				
				if (onlyChild) {
					begin = content.indexOf('>', index) + 1;
				} else {
					end = end + closeTagName1.length() + 1;
				}
			}
			
			content = content.substring(0, begin) + ctx + content.substring(end);
		} else {
			if ("super.constructor".equals(identity)) {
				content = ctx + '\n' + content;
			}
		}
		return content;
	}
}
